package com.smartbus.bus.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.smartbus.common.annotation.Log;
import com.smartbus.common.core.controller.BaseController;
import com.smartbus.common.core.domain.AjaxResult;
import com.smartbus.common.enums.BusinessType;
import com.smartbus.bus.domain.RouteStations;
import com.smartbus.bus.service.IRouteStationsService;
import com.smartbus.common.utils.poi.ExcelUtil;
import com.smartbus.common.core.page.TableDataInfo;

/**
 * 线路站点关联，定义每条线路经过的站点及其顺序关系Controller
 * 
 * @author ruoyi
 * @date 2025-09-24
 */
@RestController
@RequestMapping("/bus/route-stations")
public class RouteStationsController extends BaseController
{
    private static final Logger logger = LoggerFactory.getLogger(RouteStationsController.class);

    @Autowired
    private IRouteStationsService routeStationsService;

    /**
     * 查询线路站点关联，定义每条线路经过的站点及其顺序关系列表
     */
    @PreAuthorize("@ss.hasPermi('bus:stations:list')")
    @GetMapping("/list")
    public TableDataInfo list(RouteStations routeStations)
    {
        startPage();
        List<RouteStations> list = routeStationsService.selectRouteStationsList(routeStations);
        return getDataTable(list);
    }

    /**
     * 导出线路站点关联，定义每条线路经过的站点及其顺序关系列表
     */
    @PreAuthorize("@ss.hasPermi('bus:stations:export')")
    @Log(title = "线路站点关联，定义每条线路经过的站点及其顺序关系", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RouteStations routeStations)
    {
        List<RouteStations> list = routeStationsService.selectRouteStationsList(routeStations);
        ExcelUtil<RouteStations> util = new ExcelUtil<RouteStations>(RouteStations.class);
        util.exportExcel(response, list, "线路站点关联，定义每条线路经过的站点及其顺序关系数据");
    }

    /**
     * 获取线路站点关联，定义每条线路经过的站点及其顺序关系详细信息
     */
    @PreAuthorize("@ss.hasPermi('bus:stations:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(routeStationsService.selectRouteStationsById(id));
    }

    /**
     * 新增线路站点关联，定义每条线路经过的站点及其顺序关系
     */
    @PreAuthorize("@ss.hasPermi('bus:stations:add')")
    @Log(title = "线路站点关联，定义每条线路经过的站点及其顺序关系", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RouteStations routeStations)
    {
        return toAjax(routeStationsService.insertRouteStations(routeStations));
    }

    /**
     * 修改线路站点关联，定义每条线路经过的站点及其顺序关系
     */
    @PreAuthorize("@ss.hasPermi('bus:stations:edit')")
    @Log(title = "线路站点关联，定义每条线路经过的站点及其顺序关系", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RouteStations routeStations)
    {
        return toAjax(routeStationsService.updateRouteStations(routeStations));
    }

    /**
     * 删除线路站点关联，定义每条线路经过的站点及其顺序关系
     */
    @PreAuthorize("@ss.hasPermi('bus:stations:remove')")
    @Log(title = "线路站点关联，定义每条线路经过的站点及其顺序关系", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(routeStationsService.deleteRouteStationsByIds(ids));
    }

    /**
     * 根据线路ID删除所有站点关联
     */
    @PreAuthorize("@ss.hasPermi('bus:stations:remove')")
    @Log(title = "根据线路ID删除站点关联", businessType = BusinessType.DELETE)
    @DeleteMapping("/route/{routeId}")
    public AjaxResult removeByRouteId(@PathVariable Long routeId)
    {
        return toAjax(routeStationsService.deleteRouteStationsByRouteId(routeId));
    }

    /**
     * 批量保存线路站点关联（替换方式：先删除再添加）
     */
    @PreAuthorize("@ss.hasPermi('bus:stations:add')")
    @Log(title = "批量保存线路站点关联", businessType = BusinessType.INSERT)
    @PostMapping("/batch/{routeId}")
    public AjaxResult batchSave(@PathVariable Long routeId, @RequestBody List<RouteStations> stationsList)
    {
        try {
            int result = routeStationsService.batchSaveRouteStations(routeId, stationsList);
            return result > 0 ? success("保存成功") : error("保存失败");
        } catch (Exception e) {
            logger.error("批量保存线路站点关联失败", e);
            return error("保存失败：" + e.getMessage());
        }
    }
}
